import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/home",
      redirect: "/",
    },
    {
      path: "/",
      name: "home",
      component: () => import("@/views/Home.vue"),
      children: [
        {
          path: "/user",
          name: "user",
          component: () => import("@/views/user/index.vue"),
        },
        {
          path: "/role",
          name: "role",
          component: () => import("@/views/role/index.vue"),
        },
        {
          path: "/tag",
          name: "tag",
          component: () => import("@/views/others/Tag.vue"),
        },
      ],
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/Login.vue"),
    },
  ],
});

let whiteList = ["/login"];

//在每次路由切换的时候 需要 做判断
router.beforeEach((to, from, next) => {
  // console.log("路由切换了 ，判断是否登录 ,to.path", to);
  let username = sessionStorage.getItem("username");
  // let success: boolean = ;

  //如果没登录， 1 跳转登录页面、
  if (username || whiteList.indexOf(to.path) != -1) {
    next();
  } else {
    next("/login");
  }

  //如果已经登录 2 放行
});

export default router;
